package org.wyy.tcc;

/**
 * tcc事务管理器模版
 *
 * @author wangyuanye
 * @date 2024/7/22
 **/
public abstract class TccTxManagerTemplate {

    protected final boolean execute(TransactionalContext context) {
        if (!tryOperations(context)) {
            // 执行回滚
            cancelOperations(context);
            return false;
        }

        if (!confirmOperations(context)) {
            // 执行回滚
            cancelOperations(context);
            return false;
        }

        return true;
    }

    // 自定义执行流程
    protected abstract boolean tryOperations(TransactionalContext context);
    protected abstract boolean confirmOperations(TransactionalContext context);
    protected abstract boolean cancelOperations(TransactionalContext context);
}
